﻿using System.Windows.Forms;

namespace Xilium.CefGlue.Demo.Internals.JavascriptBinding
{
    /// <summary>
    /// 绑定测试处理器
    /// </summary>
    public class LiveV8Handler : CefV8Handler
    {
        #region 声明常量变量

        public string token { get; set; }
        public string roomId { get; set; }
        public string teacherUid { get; set; }

        #endregion 声明常量变量

        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public LiveV8Handler()
        {
            token = "";
            roomId = "";
            teacherUid = "";
        }

        #endregion 构造函数

        #region 事件

        /// <summary>
        /// 网页脚本与后台程序交互方法
        /// 提示一：如果 returnValue = null; 则会导致网页前端出现错误：Cannot read property 'constructor' of undefined
        /// 提示二：还存在其他的可能，导致导致网页前端出现错误：Cannot read property 'constructor' of undefined
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="obj">对象</param>
        /// <param name="arguments">参数</param>
        /// <param name="returnValue">返回值</param>
        /// <param name="exception">返回异常信息</param>
        /// <returns></returns>
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            string result = string.Empty;

            switch (name)
            {
                case "MyFunction":
                    MyFunction();
                    break;
                case "CallLive":
                    CallLive();
                    break;
                case "GetToken":
                    result = GetToken();
                    break;
                case "SetToken":
                    result = SetToken(arguments[0].GetStringValue());
                    break;
                case "GetRoomId":
                    result = GetRoomId();
                    break;
                case "SetRoomId":
                    result = SetRoomId(arguments[0].GetStringValue());
                    break;
                case "GetTeacherUid":
                    result = GetTeacherUid();
                    break;
                case "SetTeacherUid":
                    result = SetTeacherUid(arguments[0].GetStringValue());
                    break;
                default:
                    MessageBox.Show(string.Format("JS调用C# >> {0} >> {1} 返回值", name, obj.GetType()), "系统提示", MessageBoxButtons.OK);
                    break;
            }

            returnValue = CefV8Value.CreateString(result);
            exception = null;

            return true;
        }

        #endregion 事件

        #region 方法

        /// <summary>
        /// 我的函数
        /// </summary>
        public void MyFunction()
        {
            MessageBox.Show("ExampleAv8Handler : JS调用C# >> MyFunction >> 无11111 返回值", "系统提示", MessageBoxButtons.OK);
        }


        public string CallLive()
        {

            System.Diagnostics.Process exep = new System.Diagnostics.Process();
            exep.StartInfo.FileName = "D:\\daoshituan\\live-student\\live-student.exe";
            exep.StartInfo.Arguments = "live-student://?&" + token + "&" + roomId + "&" + teacherUid;
            exep.StartInfo.CreateNoWindow = true;
            exep.StartInfo.UseShellExecute = false;
            exep.Start();
            //exep.WaitForExit();//关键，等待外部程序退出后才能往下执行

            return "success";
        }


        /// <summary>
        /// 取值
        /// </summary>
        /// <returns></returns>
        public string GetToken()
        {
            return token;
        }

        /// <summary>
        /// 赋值
        /// </summary>
        /// <param name="value">值</param>
        /// <returns></returns>
        public string SetToken(string value)
        {
            token = value;

            return token;
        }

        public string GetRoomId()
        {
            return roomId;
        }

        public string SetRoomId(string value)
        {
            roomId = value;
            return roomId;
        }

        public string GetTeacherUid()
        {
            return teacherUid;
        }

        public string SetTeacherUid(string value)
        {
            teacherUid = value;
            return teacherUid;
        }

        #endregion 方法
    }
}